<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/13
 * Time: 11:32
 */

namespace app\admin\model;

use think\Model;
use think\Db;

class GoodsModel extends Model
{

    //商品列表
    public function goodslist($fenlei)
    {
        if ($fenlei == "0") {
            $data = Db::table('ajw_goods')
                ->alias('a')
                ->join('ajw_goods_cate h', 'a.goods_cate_id = h.goods_cate_id')
                ->paginate(10);
            return $data;
        } else {

            $data = Db::table('ajw_goods')
                ->alias('a')
                ->join('ajw_goods_cate h', 'a.goods_cate_id = h.goods_cate_id')
                ->where('h.goods_cate_id', $fenlei)
                ->paginate(10);
            return $data;
        }


    }

    // 修改查询
    public function xiugaichaxun($goods_id)
    {
        $data = Db::table('ajw_goods')
            ->where('goods_id', $goods_id)
            ->find();
        return $data;
    }

    //商品分类
    public function type()
    {
        $data = Db::table('ajw_goods_cate')
            ->select();
        return $data;
    }

    public function add($param, $image)
    {

        $goods_id = $param['goods_id'];
        $data = Db::table('ajw_goods')
            ->where('goods_id', $goods_id)
            ->select();

        //判断有没有选择图
        if (strlen($image) < 1) {
            $image = $param['img'];
        } else {
            $image = "admin/$image";
        }
        if (count($data->toArray()) > 0) {


            $map['name'] = $param['name'];
            $map['jianjie'] = $param['jianjie'];
            $map['unit'] = $param['unit'];
            $map['imgurl'] = $image;
            $map['content'] = $param['content'];
            $map['status'] = $param['status'];
            $map['goods_cate_id'] = $param['goods_cate_id'];
            $res = Db::table('ajw_goods')
                ->where('goods_id', $goods_id)
                ->update($map);
            return $res;


        } else {

            //增加
            $map['goods_id'] = $param['goods_id'];
            $map['name'] = $param['name'];
            $map['jianjie'] = $param['jianjie'];
            $map['unit'] = $param['unit'];
            $map['imgurl'] = $image;
            $map['content'] = $param['content'];
            $map['status'] = $param['status'];
            $map['goods_cate_id'] = $param['goods_cate_id'];
            $res = Db::table('ajw_goods')
                ->insert($map);
            return $res;
        }

    }


    public function goodsdel($goods_id)
    {
        $res = Db::table('ajw_goods')
            ->delete($goods_id);
        return $res;
    }
}